package interfaz2;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * TODO: A class that will handle passwords?!?! OMG IMPROVE THIS!!!
 */
/**
 * This class will be a Dialog to ask the user for the password that is needed
 * to get access to the private key used to sign a Blog.
 * 
 * Despite that is the main purpose, this Dialog can be used every time you
 * need to "ask for a password", but since this is hand-made this could had
 * (and likely it will had) lots of bugs and/or bad practices.
 * 
 * @author ole
 */
public class AskPassDialog extends DustDialog {
	// Class attributes.
	private Text text;
	
	/*
	 * Private classes.
	 */
	
	/**
	 * Listener that handles the selection of the OK button of the dialog.
	 */
	private class OkSelectionListener extends SelectionAdapter {
		@Override
		public void widgetSelected(SelectionEvent event) {
			result = text.getText();
			doExit();
		}
	}
	
	/**
	 * Listener that handles the selection of the Cancel button of the dialog.
	 */
	private class CancelSelectionListener extends SelectionAdapter {
		@Override
		public void widgetSelected(SelectionEvent event) {
			doExit();
		}
	}
	
	/*
	 * Public methods of AskPassDialog.
	 */
	
	/**
	 * Class constructor.
	 * 
	 * @param parent:
	 * 		The Shell parent of this Dialog.
	 */
	public AskPassDialog(Shell parent) {
		super(parent);
		
		Composite comp, buttons_comp;
		GridData grid;
		Button ok_button, cancel_button;
		
		shell.setText("Introduce the password to access to the private key.");
		
		// The main Composite.
		comp = new Composite(shell, SWT.BORDER);
		comp.setLayout(new GridLayout(1, true));
		grid = new GridData(SWT.LEFT, SWT.TOP, true, true);
		comp.setLayoutData(grid);
		
		// The entry text to take the password.
		text = new Text(comp, SWT.PASSWORD | SWT.BORDER);
		grid = new GridData(SWT.FILL, SWT.TOP, true, false);
		text.setLayoutData(grid);
		
		// A bottom composite for OK and Cancel buttons.
		buttons_comp = new Composite(comp, SWT.NONE);
		buttons_comp.setLayout(new GridLayout(2, true));
		grid = new GridData(SWT.FILL, SWT.FILL, true, true);
		buttons_comp.setLayoutData(grid);
		
		// The OK button.
		ok_button = new Button(buttons_comp, SWT.NONE);
		grid = new GridData(SWT.FILL, SWT.TOP, true, true);
		ok_button.setLayoutData(grid);
		ok_button.addSelectionListener(new OkSelectionListener());
		ok_button.setText("OK");
		
		// The Cancel button.
		cancel_button = new Button(buttons_comp, SWT.NONE);
		grid = new GridData(SWT.FILL, SWT.TOP, true, true);
		cancel_button.setLayoutData(grid);
		cancel_button.addSelectionListener(new CancelSelectionListener());
		cancel_button.setText("Cancel");
		
		comp.pack();
	}
}
